package com.logAndSignProvider.service.impl;

import com.logAndSignProvider.mapper.StoreMapper;
import com.logAndSignProvider.service.StoreService;
import entity.ResponseMessage;
import entity.StatusCode;
import entity.Store;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.Md5Util;
import util.RegexUtil;

import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreMapper storeMapper;

    private Logger logger = Logger.getLogger(this.getClass());

    /**
     * @Author: overlook
     * @Created by 2022年04月13日
     * @param: * @param store
     * @return: entity.ResponseMessage
     * @Description: 根据条件查询
     */
    @Override
    public ResponseMessage selectAll(Store store) {
        ResponseMessage msg = new ResponseMessage();

        try {
            //将前端传来的密码进行md5加密，与数据库中的匹配
            String password = store.getPassword();
            if (!StringUtils.isEmpty(password)) {
                String passwordMD5 = Md5Util.encryptToMD5(password);
                store.setPassword(passwordMD5);
            }
            List<Store> stores = storeMapper.selectAll(store);
            //判断结果是否为空
            if (stores == null || stores.size() == 0) {
                msg.setCode(StatusCode.ACCESSERROR);
                msg.setMessage("select store result is empty");
                logger.debug("StoreServiceImpl selectAll params:" + store + ",result:" + stores);
                return msg;
            }
            //结果不为空
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setData(stores);
            msg.setFlag(true);
            logger.debug("StoreServiceImpl selectAll params:" + store + ",result:" + stores);
            return msg;
        } catch (Exception e) {
            //异常报错情况
            e.printStackTrace();
            logger.error("StoreServiceImpl selectAll params:" + store);
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("select error");
            return msg;
        }
    }

    /**
     * @Author: overlook
     * @Created by 2022年04月13日
     * @param: * @param store
     * @return: entity.ResponseMessage
     * @Description: 新增
     */
    @Override
    public ResponseMessage insert(Store store) {
        Store queryAllStore = new Store();
        //查询全部store
        List<Store> storeList = storeMapper.selectAll(queryAllStore);
        ResponseMessage msg = new ResponseMessage();
        RegexUtil regexUtil = new RegexUtil();
        try {
            if (storeList != null && storeList.size() != 0) {
                boolean checkExist = regexUtil.checkExist(store.getUserName(), storeList);
                if (!checkExist) {
                    msg.setCode(StatusCode.ERROR);
                    msg.setFlag(false);
                    msg.setMessage("参数错误");
                    logger.debug("StoreServiceImpl insert params:" + store);
                    return msg;
                }
            }
            if (store == null || !RegexUtil.checkMobile(store.getStoreMobile()) || !RegexUtil.checkPassword(store.getPassword()) ||
                    StringUtils.isEmpty(store.getStoreAddress())) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("参数错误");
                logger.debug("StoreServiceImpl insert params:" + store);
                return msg;
            }

            //将传入的密码进行MD5加密
            String password = store.getPassword();
            if (!StringUtils.isEmpty(password)) {
                String passwordToMD5 = Md5Util.encryptToMD5(password);
                store.setPassword(passwordToMD5);
            }

            Integer insert = storeMapper.insert(store);
            //判断新增是否成功
            if (insert <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("insert failed");
                msg.setFlag(false);
                logger.debug("StoreServiceImpl insert params:" + store + ",result:" + insert);
                return msg;
            }
            //新增成功
            msg.setCode(StatusCode.OK);
            msg.setData(insert);
            msg.setFlag(true);
            msg.setMessage("ok");
            logger.debug("StoreServiceImpl insert params:" + store + ",result:" + insert);
            return msg;
        } catch (Exception e) {
            //异常报错
            e.printStackTrace();
            msg.setFlag(false);
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("insert error");
            logger.error("StoreServiceImpl insert params:" + store);
            return msg;
        }
    }

    /**
     * @Author: overlook
     * @Created by 2022年04月13日
     * @param: * @param store
     * @return: entity.ResponseMessage
     * @Description: 更新门店信息
     */
    @Override
    public ResponseMessage update(Store store) {
        ResponseMessage msg = new ResponseMessage();
        RegexUtil regexUtil = new RegexUtil();
        try {
            Store queryAllStore = new Store();
            List<Store> storeList = storeMapper.selectAll(queryAllStore);
            Store selectById = storeMapper.selectById(store.getId());
            if (!selectById.getUserName().equals(store.getUserName())) {
                boolean checkExist = regexUtil.checkExist(store.getUserName(), storeList);
                if (!checkExist) {
                    msg.setCode(StatusCode.ERROR);
                    msg.setFlag(false);
                    msg.setMessage("参数错误");
                    logger.debug("StoreServiceImpl update params:" + store);
                    return msg;
                }
            }
            if (store == null || !RegexUtil.checkMobile(store.getStoreMobile()) ||
                    StringUtils.isEmpty(store.getStoreAddress()) ||
                    StringUtils.isEmpty(store.getPassword())) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("参数错误");
                logger.debug("StoreServiceImpl update params:" + store);
                return msg;
            }

            Integer insert = storeMapper.updateById(store);
            //判断更新是否成功
            if (insert <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("update failed");
                msg.setFlag(false);
                logger.debug("StoreServiceImpl update params:" + store);
                return msg;
            }
            //更新成功
            msg.setCode(StatusCode.OK);
            msg.setMessage("ok");
            msg.setData(insert);
            msg.setFlag(true);
            return msg;
        } catch (Exception e) {
            //异常情况
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("error");
            msg.setFlag(false);
            return msg;
        }
    }

    /**
     * @Author: overlook
     * @Created by 2022年04月13日
     * @param: * @param id
     * @return: entity.ResponseMessage
     * @Description: 删除门店
     */
    @Override
    public ResponseMessage delete(String id) {
        ResponseMessage msg = new ResponseMessage();
        try {
            if (StringUtils.isEmpty(id)) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("参数错误");
                logger.debug("StoreServiceImpl delete params:" + id);
                return msg;
            }
            Integer delete = storeMapper.deleteById(id);
            //判断删除是否成功
            if (delete <= 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setMessage("delete failed");
                msg.setFlag(false);
                logger.debug("StoreServiceImpl delete params:" + id + ",result:" + delete);
                return msg;
            }
            //删除成功
            msg.setCode(StatusCode.OK);
            msg.setFlag(true);
            msg.setMessage("ok");
            msg.setData(delete);
            return msg;
        } catch (Exception e) {
            //异常报错
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setMessage("error");
            msg.setFlag(false);
            logger.error("StoreServiceImpl delete params:" + id);
            return msg;
        }
    }
}
